load("@bazel_skylib//rules:expand_template.bzl", "expand_template")
load("@bazel_skylib//rules:run_binary.bzl", "run_binary")
load("//gobject:gobject.bzl", "mkenums")

package(default_applicable_licenses = ["//:license"])

expand_template(
    name = "gnetworking_h_in",
    out = "gnetworking.h",
    substitutions = select({
        "@platforms//os:macos": {"@NAMESER_COMPAT_INCLUDE@": "#include <arpa/nameser_compat.h>"},
        "//conditions:default": {"@NAMESER_COMPAT_INCLUDE@": ""},
    }),
    template = "gnetworking.h.in",
)

[
    genrule(
        name = "gdbus_codegen_for_%s" % target.replace("-", "_"),
        srcs = srcs,
        outs = [
            "%s.c" % target,
            "%s.h" % target,
        ],
        cmd = ("$(location //gio/gdbus-2.0/codegen:gdbus_codegen) --interface-prefix {prefix} " +
               "--output-directory $$(dirname $(location {target}.c)) " +
               "--generate-c-code {target} --c-namespace {namespace} {srcs}").format(
            srcs = " ".join(["$(location %s)" % s for s in srcs]),
            namespace = namespace,
            prefix = prefix,
            target = target,
        ),
        tools = ["//gio/gdbus-2.0/codegen:gdbus_codegen"],
    )
    for prefix, target, namespace, srcs in [
        [
            "org.freedesktop.portal.",
            "xdp-dbus",
            "GXdp",
            glob(["org.freedesktop.portal.*.xml"]),
        ],
        [
            "org.",
            "gdbus-daemon-generated",
            "_G",
            ["dbus-daemon.xml"],
        ],
    ]
]

GIO_GDBUS_HDRS = [
    "gdbusauthobserver.h",
    "gcredentials.h",
    "gdbusutils.h",
    "gdbuserror.h",
    "gdbusaddress.h",
    "gdbusconnection.h",
    "gdbusmessage.h",
    "gdbusnameowning.h",
    "gdbusnamewatching.h",
    "gdbusproxy.h",
    "gdbusintrospection.h",
    "gdbusmethodinvocation.h",
    "gdbusserver.h",
    "gdbusinterface.h",
    "gdbusinterfaceskeleton.h",
    "gdbusobject.h",
    "gdbusobjectskeleton.h",
    "gdbusobjectproxy.h",
    "gdbusobjectmanager.h",
    "gdbusobjectmanagerclient.h",
    "gdbusobjectmanagerserver.h",
    "gtestdbus.h",
]

GIO_GDBUS_SRCS = [
    "gdbusutils.c",
    "gdbusaddress.c",
    "gdbusauthobserver.c",
    "gdbusauth.c",
    "gdbusauthmechanism.c",
    "gdbusauthmechanismanon.c",
    "gdbusauthmechanismexternal.c",
    "gdbusauthmechanismsha1.c",
    "gdbuserror.c",
    "gdbusconnection.c",
    "gdbusmessage.c",
    "gdbusnameowning.c",
    "gdbusnamewatching.c",
    "gdbusproxy.c",
    "gdbusprivate.c",
    "gdbusintrospection.c",
    "gdbusmethodinvocation.c",
    "gdbusserver.c",
    "gdbusinterface.c",
    "gdbusinterfaceskeleton.c",
    "gdbusobject.c",
    "gdbusobjectskeleton.c",
    "gdbusobjectproxy.c",
    "gdbusobjectmanager.c",
    "gdbusobjectmanagerclient.c",
    "gdbusobjectmanagerserver.c",
    "gtestdbus.c",
]

GIO_SETTINGS_HDRS = [
    "gsettingsbackend.h",
    "gsettingsschema.h",
    "gsettings.h",
]

GIO_SETTINGS_SRCS = [
    "gdelayedsettingsbackend.c",
    "gkeyfilesettingsbackend.c",
    "gmemorysettingsbackend.c",
    "gnullsettingsbackend.c",
    "gsettingsbackend.c",
    "gsettingsschema.c",
    "gsettings-mapping.c",
    "gsettings.c",
]

GIO_APPLICATION_HDRS = [
    "gapplication.h",
    "gapplicationcommandline.h",
    "gactiongroup.h",
    "gactionmap.h",
    "gsimpleactiongroup.h",
    "gremoteactiongroup.h",
    "gactiongroupexporter.h",
    "gdbusactiongroup.h",
    "gaction.h",
    "gpropertyaction.h",
    "gsimpleaction.h",
    "gmenumodel.h",
    "gmenu.h",
    "gmenuexporter.h",
    "gdbusmenumodel.h",
    "gnotification.h",
]

GIO_APPLICATION_SRCS = [
    "gapplication.c",
    "gapplicationcommandline.c",
    "gapplicationimpl-dbus.c",
    "gactiongroup.c",
    "gactionmap.c",
    "gsimpleactiongroup.c",
    "gremoteactiongroup.c",
    "gactiongroupexporter.c",
    "gdbusactiongroup.c",
    "gaction.c",
    "gpropertyaction.c",
    "gsimpleaction.c",
    "gmenumodel.c",
    "gmenu.c",
    "gmenuexporter.c",
    "gdbusmenumodel.c",
    "gnotification.c",
    "gnotificationbackend.c",
]

GIO_LOCAL_SRCS = [
    "ghttpproxy.c",
    "glocalfile.c",
    "glocalfileenumerator.c",
    "glocalfileinfo.c",
    "glocalfileinputstream.c",
    "glocalfilemonitor.c",
    "glocalfileoutputstream.c",
    "glocalfileiostream.c",
    "glocalvfs.c",
    "gsocks4proxy.c",
    "gsocks4aproxy.c",
    "gsocks5proxy.c",
    "thumbnail-verify.c",
]

GIO_PORTAL_SRCS = [
    "gdocumentportal.c",
    "gopenuriportal.c",
    "gmemorymonitorportal.c",
    "gnetworkmonitorportal.c",
    "gpowerprofilemonitorportal.c",
    "gproxyresolverportal.c",
    "gtrashportal.c",
    "gportalsupport.c",
    "gportalnotificationbackend.c",
    "gsandbox.c",
]

GIO_UNIX_SRCS = [
    "gfiledescriptorbased.c",
    "giounix-private.c",
    "gunixfdmessage.c",
    "gunixmount.c",
    "gunixmounts.c",
    "gunixvolume.c",
    "gunixvolumemonitor.c",
    "gunixinputstream.c",
    "gunixoutputstream.c",
    "gfdonotificationbackend.c",
    "ggtknotificationbackend.c",
]

GIO_LINUX_SRCS = [
    "gdesktopappinfo.c",
    "gnetworkmonitornetlink.c",
    "gnetworkmonitornm.c",
]

GIO_OSX_SRCS = [
    "gosxnetworkmonitor.c",
]

GIO_WIN32_SRCS = [
    "gmemorymonitorwin32.c",
    "gregistrysettingsbackend.c",
    "gwin32registrykey.c",
    "gwin32mount.c",
    "gwin32volumemonitor.c",
    "gwin32inputstream.c",
    "gwin32outputstream.c",
    "gwin32file-sync-stream.c",
    "gwin32packageparser.c",
    "gwin32networkmonitor.c",
    "gwin32networkmonitor.h",
    "gwin32notificationbackend.c",
    "gwin32sid.c",
    "gwin32sid.h",
]

GIO_BASE_SOURCES = [
    "gappinfo.c",
    "gasynchelper.c",
    "gasyncinitable.c",
    "gasyncresult.c",
    "gbufferedinputstream.c",
    "gbufferedoutputstream.c",
    "gbytesicon.c",
    "gcancellable.c",
    "gcharsetconverter.c",
    "gcontenttype.c",
    "gcontextspecificgroup.c",
    "gconverter.c",
    "gconverterinputstream.c",
    "gconverteroutputstream.c",
    "gcredentials.c",
    "gdatagrambased.c",
    "gdatainputstream.c",
    "gdataoutputstream.c",
    "gdebugcontroller.c",
    "gdebugcontrollerdbus.c",
    "gdrive.c",
    "gdummyfile.c",
    "gdummyproxyresolver.c",
    "gdummytlsbackend.c",
    "gemblem.c",
    "gemblemedicon.c",
    "gfile.c",
    "gfileattribute.c",
    "gfileenumerator.c",
    "gfileicon.c",
    "gfileinfo.c",
    "gfileinputstream.c",
    "gfilemonitor.c",
    "gfilenamecompleter.c",
    "gfileoutputstream.c",
    "gfileiostream.c",
    "gfilterinputstream.c",
    "gfilteroutputstream.c",
    "gicon.c",
    "ginetaddress.c",
    "ginetaddressmask.c",
    "ginetsocketaddress.c",
    "ginitable.c",
    "ginputstream.c",
    "gioerror.c",
    "giomodule.c",
    "giomodule-priv.c",
    "gioscheduler.c",
    "giostream.c",
    "gloadableicon.c",
    "gmarshal-internal.c",
    "gmount.c",
    "gmemorymonitor.c",
    "gmemorymonitordbus.c",
    "gmemoryinputstream.c",
    "gmemoryoutputstream.c",
    "gmountoperation.c",
    "gnativesocketaddress.c",
    "gnativevolumemonitor.c",
    "gnetworkaddress.c",
    "gnetworking.c",
    "gnetworkmonitor.c",
    "gnetworkmonitorbase.c",
    "gnetworkservice.c",
    "goutputstream.c",
    "gpermission.c",
    "gpollableinputstream.c",
    "gpollableoutputstream.c",
    "gpollableutils.c",
    "gpollfilemonitor.c",
    "gpowerprofilemonitor.c",
    "gpowerprofilemonitordbus.c",
    "gproxy.c",
    "gproxyaddress.c",
    "gproxyaddressenumerator.c",
    "gproxyresolver.c",
    "gresolver.c",
    "gresource.c",
    "gresourcefile.c",
    "gseekable.c",
    "gsimpleasyncresult.c",
    "gsimpleiostream.c",
    "gsimplepermission.c",
    "gsimpleproxyresolver.c",
    "gsocket.c",
    "gsocketaddress.c",
    "gsocketaddressenumerator.c",
    "gsocketclient.c",
    "gsocketconnectable.c",
    "gsocketconnection.c",
    "gsocketcontrolmessage.c",
    "gsocketinputstream.c",
    "gsocketlistener.c",
    "gsocketoutputstream.c",
    "gsocketservice.c",
    "gsrvtarget.c",
    "gsubprocesslauncher.c",
    "gsubprocess.c",
    "gtask.c",
    "gtcpconnection.c",
    "gtcpwrapperconnection.c",
    "gthemedicon.c",
    "gthreadedsocketservice.c",
    "gthreadedresolver.c",
    "gthreadedresolver.h",
    "gtlsbackend.c",
    "gtlscertificate.c",
    "gtlsclientconnection.c",
    "gtlsconnection.c",
    "gtlsdatabase.c",
    "gtlsfiledatabase.c",
    "gtlsinteraction.c",
    "gtlspassword.c",
    "gtlsserverconnection.c",
    "gdtlsconnection.c",
    "gdtlsclientconnection.c",
    "gdtlsserverconnection.c",
    "gunionvolumemonitor.c",
    "gunixconnection.c",
    "gunixfdlist.c",
    "gunixcredentialsmessage.c",
    "gunixsocketaddress.c",
    "gvfs.c",
    "gvolume.c",
    "gvolumemonitor.c",
    "gzlibcompressor.c",
    "gzlibdecompressor.c",
    "glistmodel.c",
    "gliststore.c",
]

GIO_HDRS = [
    "gappinfo.h",
    "gasyncinitable.h",
    "gasyncresult.h",
    "gbufferedinputstream.h",
    "gbufferedoutputstream.h",
    "gbytesicon.h",
    "gcancellable.h",
    "gcontenttype.h",
    "gcharsetconverter.h",
    "gconverter.h",
    "gconverterinputstream.h",
    "gconverteroutputstream.h",
    "gdatagrambased.h",
    "gdatainputstream.h",
    "gdataoutputstream.h",
    "gdebugcontroller.h",
    "gdebugcontrollerdbus.h",
    "gdrive.h",
    "gemblem.h",
    "gemblemedicon.h",
    "gfile.h",
    "gfileattribute.h",
    "gfileenumerator.h",
    "gfileicon.h",
    "gfileinfo.h",
    "gfileinputstream.h",
    "gfilemonitor.h",
    "gfilenamecompleter.h",
    "gfileoutputstream.h",
    "gfileiostream.h",
    "gfilterinputstream.h",
    "gfilteroutputstream.h",
    "gicon.h",
    "ginetaddress.h",
    "ginetaddressmask.h",
    "ginetsocketaddress.h",
    "ginitable.h",
    "ginputstream.h",
    "gio.h",
    "gio-autocleanups.h",
    "gioenums.h",
    "gioerror.h",
    "giomodule.h",
    "gioscheduler.h",
    "giostream.h",
    "giotypes.h",
    "gloadableicon.h",
    "gmount.h",
    "gmemoryinputstream.h",
    "gmemorymonitor.h",
    "gmemoryoutputstream.h",
    "gmountoperation.h",
    "gnativesocketaddress.h",
    "gnativevolumemonitor.h",
    "gnetworkaddress.h",
    "gnetworkmonitor.h",
    "gnetworkservice.h",
    "goutputstream.h",
    "gpermission.h",
    "gpollableinputstream.h",
    "gpollableoutputstream.h",
    "gpollableutils.h",
    "gpowerprofilemonitor.h",
    "gproxy.h",
    "gproxyaddress.h",
    "gproxyaddressenumerator.h",
    "gproxyresolver.h",
    "gresolver.h",
    "gresource.h",
    "gseekable.h",
    "gsimpleasyncresult.h",
    "gsimpleiostream.h",
    "gsimplepermission.h",
    "gsimpleproxyresolver.h",
    "gsocket.h",
    "gsocketaddress.h",
    "gsocketaddressenumerator.h",
    "gsocketclient.h",
    "gsocketconnectable.h",
    "gsocketconnection.h",
    "gsocketcontrolmessage.h",
    "gsocketlistener.h",
    "gsocketservice.h",
    "gsrvtarget.h",
    "gsubprocess.h",
    "gsubprocesslauncher.h",
    "gtask.h",
    "gtcpconnection.h",
    "gtcpwrapperconnection.h",
    "gthemedicon.h",
    "gthreadedsocketservice.h",
    "gtlsbackend.h",
    "gtlscertificate.h",
    "gtlsclientconnection.h",
    "gtlsconnection.h",
    "gtlsdatabase.h",
    "gtlsfiledatabase.h",
    "gtlsinteraction.h",
    "gtlspassword.h",
    "gtlsserverconnection.h",
    "gdtlsconnection.h",
    "gdtlsclientconnection.h",
    "gdtlsserverconnection.h",
    "gunixconnection.h",
    "gunixcredentialsmessage.h",
    "gunixfdlist.h",
    "gunixsocketaddress.h",
    "gvfs.h",
    "gvolume.h",
    "gvolumemonitor.h",
    "gzlibcompressor.h",
    "gzlibdecompressor.h",
    "glistmodel.h",
    "gliststore.h",
]

PUBLIC_HDRS = GIO_HDRS + GIO_APPLICATION_HDRS + GIO_SETTINGS_HDRS + GIO_GDBUS_HDRS

PUBLIC_HDRS_OSX = ["gosxappinfo.h"]

INTERNAL_HDRS = [
    "gappinfoprivate.h",
    "gapplicationimpl.h",
    "gasynchelper.h",
    "gcontextspecificgroup.h",
    "gcontenttypeprivate.h",
    "gcredentialsprivate.h",
    "gdbusactiongroup-private.h",
    "gdbusauth.h",
    "gdbusauthmechanism.h",
    "gdbusauthmechanismanon.h",
    "gdbusauthmechanismexternal.h",
    "gdbusauthmechanismsha1.h",
    "gdbusprivate.h",
    "gdelayedsettingsbackend.h",
    "gdocumentportal.h",
    "gdummyfile.h",
    "gdummytlsbackend.h",
    "gdummyproxyresolver.h",
    "gfileattribute-priv.h",
    "gfileinfo-priv.h",
    "ghttpproxy.h",
    "giomodule-priv.h",
    "gioprivate.h",
    "gio_trace.h",
    "glocalfile.h",
    "glocalfileenumerator.h",
    "glocalfileinfo.h",
    "glocalfileinputstream.h",
    "glocalfileiostream.h",
    "glocalfilemonitor.h",
    "glocalfileoutputstream.h",
    "glocalvfs.h",
    "gmarshal-internal.h",
    "gmemorymonitordbus.h",
    "gmemorymonitorportal.h",
    "gmountprivate.h",
    "gnetworkingprivate.h",
    "gnetworkmonitorbase.h",
    "gnetworkmonitorportal.h",
    "gnotificationbackend.h",
    "gnotification-private.h",
    "gopenuriportal.h",
    "gpollfilemonitor.h",
    "gportalsupport.h",
    "gpowerprofilemonitordbus.h",
    "gpowerprofilemonitorportal.h",
    "gproxyresolverportal.h",
    "gresourcefile.h",
    "gsandbox.h",
    "gsettingsbackendinternal.h",
    "gsettingsschema-internal.h",
    "gsettings-mapping.h",
    "gsocketinputstream.h",
    "gsocketoutputstream.h",
    "gsocks4aproxy.h",
    "gsocks4proxy.h",
    "gsocks5proxy.h",
    "gsubprocesslauncher-private.h",
    "gthreadedresolver-private.h",
    "gtrashportal.h",
    "gunionvolumemonitor.h",
    "thumbnail-verify.h",
]

INTERNAL_HDRS_UNIX = [
    "gdesktopappinfo.h",
    "gfiledescriptorbased.h",
    "giounix-private.h",
    "gunixfdmessage.h",
    "gunixinputstream.h",
    "gunixmount.h",
    "gunixmounts.h",
    "gunixoutputstream.h",
    "gunixvolume.h",
    "gunixvolumemonitor.h",
]

INTERNAL_HDRS_LINUX = [
    "gnetworkmonitornetlink.h",
    "gnetworkmonitornm.h",
]

INTERNAL_HDRS_OSX = [
    "gosxnetworkmonitor.h",
]

UPSTREAM_VERSION = module_version().split(".bcr.", 1)[0]

run_binary(
    name = "gio-visibility_h",
    outs = ["include_glib-2.0/gio/gio-visibility.h"],
    args = [
        UPSTREAM_VERSION,
        "visibility-macros",
        "GIO",
        "$(location include_glib-2.0/gio/gio-visibility.h)",
    ],
    tool = "//tools:gen-visibility-macros",
)

[
    mkenums(
        name = "glib_mkenums_for_%s" % out.replace(".", "_"),
        srcs = ["gnetworking.h"] + PUBLIC_HDRS + select({
            "@platforms//os:macos": PUBLIC_HDRS_OSX,
            "//conditions:default": [],
        }),
        out = out,
        template = "%s.template" % out,
    )
    for out in [
        "gioenumtypes.c",
        "gioenumtypes.h",
    ]
]

[expand_template(
    name = "public_hdr_" + hdr,
    out = "include_glib-2.0/gio/" + hdr,
    substitutions = {},
    template = hdr,
) for hdr in (PUBLIC_HDRS + [
    "gioenumtypes.h",
    "gnetworking.h",
] + PUBLIC_HDRS_OSX)]

cc_library(
    name = "gio_internal_textual_hdrs",
    textual_hdrs = INTERNAL_HDRS + select({
        "@platforms//os:linux": INTERNAL_HDRS_UNIX + INTERNAL_HDRS_LINUX,
        "@platforms//os:macos": INTERNAL_HDRS_UNIX + INTERNAL_HDRS_OSX,
        "//conditions:default": [],
    }) + ["strinfo.c"],
)

REPO_ROOT = package_relative_label(":BUILD.bazel").workspace_root

INCLUDE_PREFIX = REPO_ROOT if REPO_ROOT else "."

cc_library(
    name = "gio",
    srcs = GIO_BASE_SOURCES + select({
        "@platforms//os:linux": ["gcontenttype-fdo.c"],
        "@platforms//os:macos": [],
        "@platforms//os:windows": ["gcontenttype-win32.c"],
        "//conditions:default": [],
    }) + select({
        "@platforms//os:linux": GIO_UNIX_SRCS + GIO_LINUX_SRCS,
        "@platforms//os:macos": GIO_UNIX_SRCS + GIO_OSX_SRCS,
        "@platforms//os:windows": GIO_WIN32_SRCS + ["gwin32appinfo.c"],
        "//conditions:default": [],
    }) + GIO_APPLICATION_SRCS + GIO_SETTINGS_SRCS + GIO_GDBUS_SRCS + select({
        "@platforms//os:windows": [],
        "//conditions:default": GIO_PORTAL_SRCS,
    }) + GIO_LOCAL_SRCS + PUBLIC_HDRS + select({
        "@platforms//os:macos": PUBLIC_HDRS_OSX,
        "//conditions:default": [],
    }) + select({
        "@platforms//os:linux": glob([
            "inotify/*.c",
            "inotify/*.h",
        ]),
        "@platforms//os:macos": glob([
            "kqueue/*.c",
            "kqueue/*.h",
        ]),
        "//conditions:default": [],
    }) + [
        "gdbus-daemon-generated.c",
        "gdbus-daemon-generated.h",
        "gdbusdaemon.c",
        "gdbusdaemon.h",
        "gioenumtypes.c",
        "gioenumtypes.h",
        "gnetworking.h",
        "xdp-dbus.c",
        "xdp-dbus.h",
        "//subprojects/gvdb",
    ],
    hdrs = [":public_hdr_" + hdr for hdr in (PUBLIC_HDRS + [
               "gioenumtypes.h",
               "gnetworking.h",
           ])] +
           select({
               "@platforms//os:macos": [":public_hdr_" + hdr for hdr in PUBLIC_HDRS_OSX],
               "//conditions:default": [],
           }) + [
        ":gio-visibility_h",
    ],
    copts = [
        "-I" + INCLUDE_PREFIX,
        "-I%s/gio" % (INCLUDE_PREFIX,),
        "-I%s/glib" % (INCLUDE_PREFIX,),
        "-I%s/subprojects/gvdb" % (INCLUDE_PREFIX,),
        "-I$(BINDIR)/%s" % (INCLUDE_PREFIX,),
        "-I$(BINDIR)/%s/gio" % (INCLUDE_PREFIX,),
    ],
    implementation_deps = [
        ":gio_internal_textual_hdrs",
        "//:config_h",
        "//gio/xdgmime",
        "@zlib",
    ] + select({
        "@platforms//os:linux": [],
        "//conditions:default": ["//glib:stub_intl"],
    }),
    includes = ["include_glib-2.0"],
    linkopts = select({
        "@platforms//os:linux": ["-lresolv"],
        "//conditions:default": [],
    }),
    linkstatic = True,
    local_defines = [
        "G_LOG_DOMAIN='\"GLib-GIO\"'",
        "GIO_LAUNCH_DESKTOP='\"gio-launch-desktop-unset\"'",
        "GIO_MODULE_DIR='\"/usr/lib/glib/gio/modules\"'",
        "LOCALSTATEDIR='\"\"'",
        "GIO_COMPILATION",
    ],
    visibility = ["//visibility:public"],
    deps = [
        "//glib",
        "//gmodule",
        "//gobject",
    ],
)
